/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sarco.bwi.lib;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author iktsup
 */
public class Config {
    private String[] configF;
    private String mysql_host;
    private String mysql_user;
    private String mysql_pass;
    private String mysql_db;
    private boolean useMysql;
    private String[] nodes;
    private String blenderPath;
    private String blend;
    private String format;
    private String ouput;
    private String master;
    private int port;
    private int startFrame;
    private int endFrame;
    
    private static final String SPLIT           = ";";
    private static final String COMMENT         = "#";
    private static final String NODE            = "node=";
    private static final String BLENDER_PATH    = "blender=";
    private static final String BLEND_FILE      = "blend=";
    private static final String FORMAT          = "format=";
    private static final String OUTPUT          = "output=";
    private static final String MASTER          = "master=";
    private static final String PORT            = "port=";
    private static final String START_FRAME     = "start=";
    private static final String END_FRAME       = "end=";
    private static final String MYSQL_HOST      = "mysql_host=";
    private static final String MYSQL_USER      = "mysql_user=";
    private static final String MYSQL_PASS      = "mysql_pass=";
    private static final String MYSQL_DATABASE  = "mysql_db=";
    private static final String MYSQL_USE       = "mysql=";

    /**
     *
     */
    public static final String DEFAULT_CONFIG = "default.config";

    /**
     *
     */
    public Config() {
        
    }

    /**
     *
     * @param config
     */
    public void loadConfig(String config) {
        try {
            System.out.println("Loading config: " + config);
            Data.getMessages().add("Loading config: " + config);
            BufferedReader in = new BufferedReader(new FileReader(config));
            String conf = "";
            String s = in.readLine();
            while (s != null) {
                conf += s + Config.SPLIT;
                s = in.readLine();
            }
            // <START> Clean up config :
            conf = conf.replaceAll("\"", "");
            // <END>
            this.configF = conf.split(Config.SPLIT);
            this.readConfig();
        } catch (IOException ex) {
            Data.getMessages().add(ex.getMessage());
        }
    }

    private void readConfig() {
        System.out.println("Reading config!");
        Data.getMessages().add("Reading config!");
        String node = "";

        Data.getMessages().add("DEBUG: Length of config: " + this.configF.length);

        for (int i = 0; i < this.configF.length; i++) {
            String line = this.configF[i];
            if (line.startsWith(Config.COMMENT)) { /* -- SKIP -- */ }
            else if(line.startsWith(Config.NODE)) {
                String regExp = "\\d{1,3}\\-\\d{1,3}";
                boolean hasRegex = false;
                String s = line.substring(Config.NODE.length());
                Pattern p = Pattern.compile(regExp);
                Matcher matcher = p.matcher(s);
                hasRegex = matcher.find();
                if (hasRegex) {
                    String ip = s.replaceAll(regExp, "");
                    String s2 = matcher.group();
                    String[] nums = s2.split("\\-");
                    int y = 0;
                    int z = 0;

                    y = Integer.parseInt(nums[0]);
                    z = Integer.parseInt(nums[1]);

                    while (y <= z) {
                        String sNode = ip + y + Config.SPLIT;
                        node += sNode;
                        y++;
                    }
                } else {
                    node += s + Config.SPLIT;
                }
            } else if (line.startsWith(Config.BLENDER_PATH)) {
                String s = line.substring(Config.BLENDER_PATH.length());
                this.setBlenderPath(s);
            } else if (line.startsWith(Config.BLEND_FILE)) {
                String s = line.substring(Config.BLEND_FILE.length());
                this.setBlend(s);
            } else if (line.startsWith(Config.FORMAT)) {
                String s = line.substring(Config.FORMAT.length());
                this.setFormat(s);
            } else if (line.startsWith(Config.OUTPUT)) {
                String s = line.substring(Config.OUTPUT.length());
                this.setOuput(s);
            } else if (line.startsWith(Config.MASTER)) {
                String s = line.substring(Config.MASTER.length());
                this.setMaster(s);
            } else if (line.startsWith(Config.PORT)) {
                String s = line.substring(Config.PORT.length());
                this.setPort(Integer.parseInt(s));
            } else if (line.startsWith(Config.START_FRAME)) {
                String s = line.substring(Config.START_FRAME.length());
                this.setStartFrame(Integer.parseInt(s));
            } else if (line.startsWith(Config.END_FRAME)) {
                String s = line.substring(Config.END_FRAME.length());
                this.setEndFrame(Integer.parseInt(s));
            } else if (line.startsWith(Config.MYSQL_HOST)) {
                String s = line.substring(Config.MYSQL_HOST.length());
                this.setMysql_host(s);
            } else if (line.startsWith(Config.MYSQL_USER)) {
                String s = line.substring(Config.MYSQL_USER.length());
                this.setMysql_user(s);
            } else if (line.startsWith(Config.MYSQL_PASS)) {
                String s = line.substring(Config.MYSQL_PASS.length());
                this.setMysql_pass(s);
            } else if (line.startsWith(Config.MYSQL_DATABASE)) {
                String s = line.substring(Config.MYSQL_DATABASE.length());
                this.setMysql_db(s);
            } else if (line.startsWith(Config.MYSQL_USE)) {
                String s = line.substring(Config.MYSQL_USE.length());
                this.setUseMysql(Boolean.parseBoolean(s));
            }

            Data.getMessages().add(line);
        }

        this.setNodes(node.split(Config.SPLIT));
    }

    public String getMysql_user() {
        return mysql_user;
    }

    public void setMysql_user(String mysql_user) {
        this.mysql_user = mysql_user;
    }

    public String getMysql_pass() {
        return mysql_pass;
    }

    public void setMysql_pass(String mysql_pass) {
        this.mysql_pass = mysql_pass;
    }

    public String getMysql_db() {
        return mysql_db;
    }

    public void setMysql_db(String mysql_db) {
        this.mysql_db = mysql_db;
    }

    public String[] getNodes() {
        return nodes;
    }

    public void setNodes(String[] nodes) {
        this.nodes = nodes;
    }

    public String getBlenderPath() {
        return blenderPath;
    }

    public void setBlenderPath(String blenderPath) {
        this.blenderPath = blenderPath;
    }

    public String getBlend() {
        return blend;
    }

    public void setBlend(String blend) {
        this.blend = blend;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getOuput() {
        return ouput;
    }

    public void setOuput(String ouput) {
        this.ouput = ouput;
    }

    public String getMaster() {
        return master;
    }

    public void setMaster(String master) {
        this.master = master;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getStartFrame() {
        return startFrame;
    }

    public void setStartFrame(int startFrame) {
        this.startFrame = startFrame;
    }

    public int getEndFrame() {
        return endFrame;
    }

    public void setEndFrame(int endFrame) {
        this.endFrame = endFrame;
    }

    /**
     * @return the useMysql
     */
    public boolean isUseMysql() {
        return useMysql;
    }

    /**
     * @param useMysql the useMysql to set
     */
    public void setUseMysql(boolean useMysql) {
        this.useMysql = useMysql;
    }

    /**
     * @return the mysql_host
     */
    public String getMysql_host() {
        return mysql_host;
    }

    /**
     * @param mysql_host the mysql_host to set
     */
    public void setMysql_host(String mysql_host) {
        this.mysql_host = mysql_host;
    }
}
